﻿#region
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using Y2M.Components;
using Y2M.Controls;
using Y2M.Properties;

#endregion

namespace Y2M.Forms
{
    public partial class MainForm2 : Form
    {
        public MainForm2()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode || RunTimeChecker.IsRunTime == false)
                return;

            if (string.IsNullOrEmpty(Settings.Default.DownloadRoot))
                Settings.Default.DownloadRoot = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            Text += " - " + Utility.GetPublishVersionText();

            ClipboardMonitor.Start();
            ClipboardMonitor.OnClipboardChange += ClipboardMonitor_ClipboardChange;

#if DEBUG
            TopMost = false;
#endif
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            ClipboardMonitor.Stop();

            Settings.Default.Save();

            base.OnClosing(e);
        }

        private void ClipboardMonitor_ClipboardChange(ClipboardFormat format, object data)
        {
            string url = data as string;

            if (url == null)
                return;

            this.InvokeIfNeeded((x => OnNewUrlCopied(x)), url);
        }

        private void OnNewUrlCopied(string url)
        {
            var control = new ItemControl(url);
            control.Height = 75;
            control.Margin = new Padding(5);
            control.Dock = DockStyle.Top;
            pnlClip.Controls.Add(control);
            control.BringToFront();

            control.WrongClipCopied += ItemControl_WrongClipCopied;
        }

        private void ItemControl_WrongClipCopied(object sender, ItemControl.WrongClipCopiedEventArgs e)
        {
            pnlClip.Controls.Remove(e.ItemControl);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = pnlClip.Controls.Count - 1; i >= 0; i--)
                if (((ItemControl) pnlClip.Controls[i]).Finished)
                    pnlClip.Controls.RemoveAt(i);
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Process.Start("http://kimgwajang.tistory.com/266");
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            using (var form = new SettingForm())
            {
                form.ShowDialog();
            }
        }

        private void trbOpacity_Scroll(object sender, EventArgs e)
        {
            Opacity = trbOpacity.Value * 0.01;
        }

        private void btnAdvancedMode_Click(object sender, EventArgs e)
        {
            TopMost = false;

            using (var form = new MainForm())
            {
                form.ShowDialog();
            }

            TopMost = true;
        }
    }
}